package com.floreantpos.jreports;

import java.util.ArrayList;
import java.util.List;

import com.floreantpos.main.Application;
import com.floreantpos.model.Ticket;
import com.floreantpos.model.TicketItem;
import com.floreantpos.model.TicketItemModifier;
import com.floreantpos.model.TicketItemModifierGroup;
import java.util.Iterator;

public class TicketDataSource extends AbstractReportDataSource {

  public TicketDataSource() {
    super(new String[]{"itemQty", "itemName", "itemUnitPrice", "itemSubtotal"});
  }

  public TicketDataSource(Ticket ticket) {
    super(new String[]{"itemQty", "itemName", "itemUnitPrice", "itemSubtotal"});

    setTicket(ticket);
  }

  private void setTicket(Ticket ticket) {
    ArrayList<Row> rows = new ArrayList<Row>();
    ArrayList<Row> rowsMod = new ArrayList<Row>();

    List<TicketItem> ticketItems = ticket.getTicketItems();

    if (!ticketItems.isEmpty()) {
      for (TicketItem ticketItem : ticketItems) {
        int pizzaFlavorQty = 0;
        List<TicketItemModifierGroup> modifierGroups = ticketItem.getTicketItemModifierGroups();
        if (modifierGroups != null) {
          for (TicketItemModifierGroup modifierGroup : modifierGroups) {
            List<TicketItemModifier> modifiers = modifierGroup.getTicketItemModifiers();
            if (modifiers != null) {
              for (TicketItemModifier modifier : modifiers) {
                if (modifier.isPizzaModifier()) {
                  pizzaFlavorQty += 1;
                }
              }
            }
          }
          for (TicketItemModifierGroup modifierGroup : modifierGroups) {
            List<TicketItemModifier> modifiers = modifierGroup.getTicketItemModifiers();
            if (modifiers != null) {
              for (TicketItemModifier modifier : modifiers) {

                if (modifier.getTotalAmount() == 0 && !modifier.isPizzaModifier()) {
                  continue;
                }

                String name = "  + " + modifier.getName();

                if (modifier.getModifierType() == TicketItemModifier.EXTRA_MODIFIER) {
                  name = "  ++ " + modifier.getName();
                }

                Row rowMod = new Row();
                rowMod.setItemCount(0);

                if (modifier.isPizzaModifier()) {
                  if (pizzaFlavorQty > 1) {
                    rowMod.setItemName("  " + modifier.getName() + " (1/" + pizzaFlavorQty + ")");
                  } else {
                    rowMod.setItemName("  " + modifier.getName() + " (" + pizzaFlavorQty + ")");
                  }
                } else {
                  rowMod.setItemName(name + " (" + modifier.getItemCount() + ")");
                }

                rowMod.setUnitPrice(Double.valueOf(0));
                rowMod.setSubtotalAmount(Double.valueOf(0));
                rowsMod.add(rowMod);
              }
            }
          }
        }
        Row row = new Row();
        row.setItemCount(ticketItem.getItemCount());
        row.setItemName(ticketItem.getName());
        row.setUnitPrice(ticketItem.getSubtotalAmount() / ticketItem.getItemCount());
        row.setSubtotalAmount(ticketItem.getSubtotalAmount());
        rows.add(row);
        if (!rowsMod.isEmpty()) {
          for (Iterator iter = rowsMod.iterator(); iter.hasNext();) {
            Row rowMod = (Row) iter.next();
            rows.add(rowMod);
          }
          rowsMod.clear();
        }
      }
    }
    if (ticket.getDiscountAmount() > 0) {
      Row row = new Row();
      row.setItemCount(1);
      row.setItemName("Desconto");
      row.setUnitPrice(ticket.getDiscountAmount() * -1);
      row.setSubtotalAmount(ticket.getDiscountAmount() * -1);
      rows.add(row);
    }
    setRows(rows);
  }

  public Object getValueAt(int rowIndex, int columnIndex) {
    Row item = (Row) rows.get(rowIndex);

    switch (columnIndex) {
      case 0:
        return String.valueOf(item.getItemCount());

      case 1:
        return item.getItemName();

      case 2:
        return Application.formatNumber(item.getUnitPrice());

      case 3:
        return Application.formatNumber(item.getSubtotalAmount());
    }

    return null;
  }

  private class Row {

    private int itemCount;
    private String itemName;
    private double unitPrice;
    private double subtotalAmount;

    public Row() {
      super();
    }

    public Row(int itemCount, String itemName, double unitPrice, double subtotalAmount) {
      super();
      this.itemCount = itemCount;
      this.itemName = itemName;
      this.unitPrice = unitPrice;
      this.subtotalAmount = subtotalAmount;
    }

    public int getItemCount() {
      return itemCount;
    }

    public void setItemCount(int itemCount) {
      this.itemCount = itemCount;
    }

    public String getItemName() {
      return itemName;
    }

    public void setItemName(String itemName) {
      this.itemName = itemName;
    }

    public double getSubtotalAmount() {
      return subtotalAmount;
    }

    public void setSubtotalAmount(double subtotalAmount) {
      this.subtotalAmount = subtotalAmount;
    }

    public double getUnitPrice() {
      return unitPrice;
    }

    public void setUnitPrice(double unitPrice) {
      this.unitPrice = unitPrice;
    }
  }
}
